<?php
namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use app\common\lib\Show;
use app\admin\validate\Login as LoginValidate;
use app\admin\business\Login  as LoginBusiness;
use think\exception\ValidateException;
use think\Exception;
use edward\captcha\facade\CaptchaApi;
use think\facade\Cache;

/**
 * @Apidoc\Title("超级用户登陆")
 * @Apidoc\Group("Login")
 * @Apidoc\Sort("2")
 */

class Login extends AdminBaseController
{
  /**
   * @Apidoc\Title("超级用户用户名密码登陆")
   * @Apidoc\Desc("超级用户用户名密码登陆使用这个接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/login")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("登陆")
   * @Apidoc\Param("username", type="string",require=true, desc="用户名" )
   * @Apidoc\Param("password", type="string",require=true, desc="密码" )
   * @Apidoc\Param("captcha", type="string",require=true, desc="验证码" )
   * @Apidoc\Param("captchaKey", type="string",require=true, desc="验证码Key" )
   * @Apidoc\Returned("data", type="object", desc="业务数据",
   *    @Apidoc\Returned("id", type="int", desc="用户ID"),
   *    @Apidoc\Returned( ref="auth"),
   *    @Apidoc\Returned("username", type="string", desc="用户名"),
   *    @Apidoc\Returned("avatar", type="string", desc="用户头像"),
   *    @Apidoc\Returned("auths", type="Array", desc="用户权限")
   * )
   */
  public function login(){
    if (!$this->request->isPost()) {
      return Show::error('错误的请求'); 
    }
    $qurey_info = $this->request->param();
    try {
      $validate_qurey_info = [
        'username'=>$qurey_info['username'],
        'password'=>$qurey_info['password'],
        'captcha'=>[
          'code'=>$qurey_info['captcha'],
          'key'=>$qurey_info['captchaKey']
        ]
      ];
      validate(LoginValidate::class)->check($validate_qurey_info);
    }catch (ValidateException $validateException){
      return Show::error($validateException->getError());
    }
    try {
      // 查找用户信息
      $super_user = $this->super_user_model->getSuperUserByUsername($qurey_info['username']);
      if (empty($super_user)|| $super_user->password !== md5($qurey_info['password'])){
        return Show::error('用户名或密错误'); 
      } else {
        $result = LoginBusiness::login($super_user);
        if (!empty( $result['id'])) return Show::success($result,'登陆成功！',200);
      }
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("验证码")
   * @Apidoc\Desc("超级用户账号密码登陆获取验证码接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/captcha")
   * @Apidoc\Method("GET")
   * @Apidoc\Tag("验证码")
   * @Apidoc\Returned("data", type="object", desc="业务数据",
   *    @Apidoc\Returned("base64", type="string", desc="验证码url"),
   *    @Apidoc\Returned("key", type="string", desc="验证码key")
   * )
   */
  public function captcha()
  {
    try {
      $captcha = CaptchaApi::create();
      $result = [
        'base64'=>$captcha['base64'],
        'key'=>$captcha['key'],
        // 'md5'=>$captcha['md5'],
        // 'code'=>$captcha['code'],
      ];
      return Show::success($result,'获取验证码成功！',200);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  // 退出登陆接口 
  /**
   * @Apidoc\Title("超级用户退出登陆")
   * @Apidoc\Desc("超级用户退出登陆接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/login_out")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("退出登陆")
   * @Apidoc\Returned("data", type="boolean", desc="业务数据")
   */
  public function loginOut(){
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    try {
      $token = $this->request->header('token');
      // 删除票据
      Cache::store('redis')->delete($token);
      return Show::success(true, '已经退出！',200);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
}
